// two/admin/admin.js
const api = require("../../api/mop");
const { err, toast } = require("../../utils/wxapi");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    list: [],
    total: 0,
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getlist();
  },
  async getlist() {
    try {
      const { page, list } = this.data;
      const res = await api.threeUser({ page });
      if (res.respCode === 200) {
        const newlist = JSON.parse(res.source);
        const total = res.pageTotal;
        const updatelist = page > 1 ? [...list, ...newlist] : newlist;
        this.setData({ list: updatelist, total });
      }
    } catch (error) {
      err(error);
    }
  },
  add() {
    wx.showModal({
      title: "请输入用户ID",
      editable: true,
      complete: async (res) => {
        console.log(res);
        if (res.confirm) {
          try {
            const data = { userId: res.content };
            const resp = await api.threeAdd(data);
            if (resp.respCode === 200) {
              toast("添加成功");
              this.getlist();
            } else {
              toast(resp.msg);
            }
          } catch (error) {
            err(error);
          }
        }
      },
    });
  },
  async del(e) {
    try {
      const userId = e.currentTarget.dataset.userid;
      console.log(userId);
      const data = { userId, role: "1" };
      const res = await api.delThree(data);
      console.log(res);
      if (res.respCode === 200) {
        toast("删除成功");
        this.getlist();
      } else {
        toast(res.msg);
      }
    } catch (error) {
      err(error);
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const { page, total, list } = this.data;
    if (list.length < total) {
      this.setData({ page: page + 1 });
      this.getlist();
    } else {
      toast("暂无更多内容");
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
